<template>
  <j-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    switchFullscreen
    @ok="handleSubmit"
    @cancel="close"
    style="top:50px"
    cancelText="关闭">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="配方编码">
              <a-input placeholder="请输入配方编码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="配方号">
              <a-input placeholder="请输入配方号" v-model="queryParam.no"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="配方名称">
                <a-input placeholder="请输入配方名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="配方类别">
                <j-dict-select-tag placeholder="请选择配方类别" v-model="queryParam.category" dictCode="pf_category"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="配方类型">
                <j-dict-select-tag placeholder="请选择配方类型" v-model="queryParam.type" dictCode="pf_type"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-card :bordered='false'>
      <a-row :gutter="8">
        <a-col :span="12">

      <!-- 查询区域-END -->

      <!-- table区域-begin -->
      <div>
        <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
          <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
          style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
          <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
        </div>

        <a-table
          ref='table'
          size='middle'
          bordered
          rowKey='id'
          class='j-table-force-nowrap'
          :scroll='{x:true}'
          :columns='columns'
          :dataSource='dataSource'
          :pagination='ipagination'
          :loading='loading'
          :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval}'
          @change='handleTableChange'>
        </a-table>
      </div>
        </a-col>
        <a-col :span="12">
          <a-table bordered :data-source="model.detailList"
                   tableLayout='fixed'
                   :columns="materielColumns"
                   :rowKey="record=>record.detailNo"
                   :pagination="false">
            <!--              <template slot="formula" slot-scope="text, record">-->
            <!--                <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />-->
            <!--              </template>-->
          </a-table>
        </a-col>
      </a-row>
    </a-card>
  </j-modal>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import ActApplyBtn from "@views/flowable/components/ActApplyBtn";
import ActCancelBtn from "@views/flowable/components/ActCancelBtn";
import ActHandleBtn from "@views/flowable/components/ActHandleBtn";
import ActHistoricDetailBtn from "@views/flowable/components/ActHistoricDetailBtn";
import { getAction } from '@api/manage'
import {mixinDevice} from '@/utils/mixin'
export default {
  name: 'GoodsFormula',
  mixins: [JeecgListMixin,mixinDevice],
  components: {
    ActApplyBtn,
    ActCancelBtn,
    ActHandleBtn,
    ActHistoricDetailBtn
  },
  props: {
    category: {
      type: String
    },
    modalWidth: {
      type: Number,
      required: false,
      default: 1250
    },
    checkval: {
      type: String,
      default: 'radio'
    }
  },
  data() {
    return {
      description: '物料配方管理页面',
      visible: false,
      title: '选择物料配方',
      model: {
        detailList:[],
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '年',
          align: "center",
          dataIndex: 'year',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '配方编码',
          align: "center",
          dataIndex: 'code'
        },
        {
          title: '配方号',
          align: "center",
          dataIndex: 'no'
        },
        {
          title: '配方名称',
          align: "center",
          dataIndex: 'name'
        },
        {
          title: '配方类别',
          align: "center",
          dataIndex: 'category_dictText'
        },
        {
          title: '配方类型',
          align: "center",
          dataIndex: 'type_dictText'
        },
        {
          title: '制单人',
          align: "center",
          dataIndex: 'createByName'
        },
        {
          title: '制单部门',
          align: "center",
          dataIndex: 'deptName'
        },
        {
          title: '制单日期',
          align: "center",
          dataIndex: 'createTime'
        },
      ],
      materielColumns: [
        {
          title: '编号',
          dataIndex: 'detailNo',
        },
        {
          title: '物料名称',
          dataIndex: 'materielName',
        },
        {
          title: '规格',
          dataIndex: 'spec',
        },
        {
          title: '是否客户自带料',
          dataIndex: 'isClientOwn',
        },
        {
          title: '配方%',
          dataIndex: 'formula',
        },
        {
          title: '实际数量',
          dataIndex: 'actualQuantity',
        },
      ],
      url: {
        list: "/base/erpGoodsFormula/list",
        delete: "/base/erpGoodsFormula/delete",
        deleteBatch: "/base/erpGoodsFormula/deleteBatch",
        exportXlsUrl: "/base/erpGoodsFormula/exportXls",
        importExcelUrl: "base/erpGoodsFormula/importExcel",
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()

  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleSubmit() {
      console.log("点击确定了")
      console.log('获取的数据',this.selectedRowKeys)
      this.getFormulaSelectRows();
      // this.searchReset(0)
      this.close();
    },
    add() {
      this.showModal();
    },
    showModal() {
      this.visible = true;
      this.loadData();
    },
    getFormulaSelectRows() {
      let selectedRow = null;
      if(this.checkval=='radio'){
        selectedRow = this.selectionRows[0];
      }else{
        selectedRow = this.selectedRowKeys
      }
      // let selectedRow = this.selectionRows[0];
      console.log("!!!"+selectedRow)
      //传递选中的数据
      this.$emit("goodsOk", selectedRow);
    },
    initDictConfig() {
    },
    close() {
      this.searchReset(false);
      this.visible = false;
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({type: 'string', value: 'code', text: '配方编码', dictCode: ''})
      fieldList.push({type: 'string', value: 'no', text: '配方号', dictCode: ''})
      fieldList.push({type: 'string', value: 'name', text: '配方名称', dictCode: ''})
      fieldList.push({type: 'string', value: 'category', text: '配方类别', dictCode: 'pf_category'})
      fieldList.push({type: 'string', value: 'type', text: '配方类型', dictCode: 'pf_type'})
      fieldList.push({type: 'date', value: 'year', text: '年'})
      this.superFieldList = fieldList
    },
    onSelectChange(selectedRowKeys, selectedRows){
      console.log(selectedRows);
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectedRows;
      getAction('/base/erpGoodsFormulaDetail/list', {formulaCode:selectedRows[0].code}).then(res => {
        this.model.detailList = res.result;
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>